package cyclon.system.peer.cyclon;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import common.peer.PeerAddress;
import common.peer.PeerInfo;

public class PeerDescriptor implements Comparable<PeerDescriptor>, Serializable {
	private static final long serialVersionUID = 1906679375438244117L;
	private final PeerAddress peerAddress;
	private final PeerInfo peerInfo;
	private int age;

//-------------------------------------------------------------------
	public PeerDescriptor(PeerAddress peerAddress, PeerInfo peerInfo) 
	{	this.peerInfo = copyPI(peerInfo);
		this.peerAddress = peerAddress;
		this.age = 0;
	}
	
//-------------------------------------------------------------------Edited
	
    private PeerInfo copyPI(PeerInfo Old)
    {
    	ArrayList<PeerAddress> newParents = new ArrayList<PeerAddress>(Old.getParents());
    	HashMap<PeerAddress, Integer> newParentHCs = new HashMap<PeerAddress, Integer>(Old.getParentHCs());
    	//System.out.println(newParentHCs);
    	PeerInfo New = new PeerInfo(Old.getPeerAddress(), newParents);
    	New.setParentHCs(newParentHCs);
    	New.setAge(Old.getAge());
    	New.setHopCount(Old.getHopCount());
    	New.setSecondHopCount(Old.getSecondHopCount());
    	return New;
    }

//-------------------------------------------------------------------
	public int incrementAndGetAge() {
		age++;
		return age;
	}

//-------------------------------------------------------------------
	public int getAge() {
		return age;
	}

//-------------------------------------------------------------------
	public PeerAddress getPeerAddress() {
		return peerAddress;
	}
	
	//-------------------------------------------------------------------
	public PeerInfo getPeerInfo() {
		return peerInfo;
	}

//-------------------------------------------------------------------
	@Override
	public int compareTo(PeerDescriptor that) {
		if (this.age > that.age)
			return 1;
		if (this.age < that.age)
			return -1;
		return 0;
	}

//-------------------------------------------------------------------
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((peerAddress == null) ? 0 : peerAddress.hashCode());
		return result;
	}

//-------------------------------------------------------------------
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PeerDescriptor other = (PeerDescriptor) obj;
		if (peerAddress == null) {
			if (other.peerAddress != null)
				return false;
		} else if (!peerAddress.equals(other.peerAddress))
			return false;
		return true;
	}

//-------------------------------------------------------------------
	@Override
	public String toString() {
		return peerAddress + "";
	}
	
}
